package com.gzdict.common.ext

import android.content.Context
import android.util.TypedValue
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

/**
 * 描述: 简单语法糖
 * @author: create by shang
 * date:2024/3/6 0006 14
 */


fun Date.format(format: String): String {
    val sdf = SimpleDateFormat(format, Locale.CHINA)
    return sdf.format(this)
}

fun String.dateFormat(format: String = "yyyy-MM-dd HH:mm:ss"): Date? {
    return try {
        val sdf = SimpleDateFormat(format, Locale.CHINA)
        sdf.parse(this)
    } catch (e: Exception) {
        null
    }
}

fun Number.dp(context: Context): Float {
    val density = context.resources.displayMetrics.density
    return this.toFloat() * density + 0.5f
}

fun Float.sp(context: Context): Float {
    return TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_SP,
        this,
        context.resources.displayMetrics
    )
}